package com.shixun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shixun.domain.Dormitory;
import com.shixun.domain.Sd;
import com.shixun.domain.Student;
import com.shixun.dto.StudentDto;
import com.shixun.exception.exception.DormException;
import com.shixun.mapper.DormitoryMapper;
import com.shixun.service.DormitoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shixun.service.SdService;
import com.shixun.service.StudentService;
import com.shixun.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author shixun
 * @since 2023-06-10
 */
@Service
@Slf4j
public class DormitoryServiceImpl extends ServiceImpl<DormitoryMapper, Dormitory> implements DormitoryService {
    @Autowired
    private StudentService studentService;
    @Autowired
    private SdService sdService;
    @Autowired
    private DormitoryMapper dormitoryMapper;


//    @Override
//    @Transactional
//    public R assignment() {
//        List<Student> students = studentService.list();
//        List<Long> ids = new ArrayList<>();
//        for (Student student : students) {
//            ids.add(student.getId());
//        }
//        List<Dormitory> dormitories = this.list();
//        for (Student student : students) {
//            //先判断这个学生是否有宿舍了
//            LambdaQueryWrapper<Sd> queryWrapper = new LambdaQueryWrapper<>();
//            queryWrapper.eq(Sd::getSid, student.getId());
//            Sd sd = sdService.getOne(queryWrapper);
//            //如果为空，为学生添加宿舍
//            if (sd == null) {
//                //遍历宿舍，看看哪间宿舍还有剩
//                for (Dormitory dormitory : dormitories) {
//                    //如果宿舍当前容量小于最大容量，就把学生加进去
//                    if (dormitory.getNowCapacity() < dormitory.getMaxCapacity()) {
//                        //把宿舍容量加一
//                        dormitory.setNowCapacity(dormitory.getNowCapacity() + 1);
//                        this.updateById(dormitory);
//                        //添加学生宿舍表
//                        Long did = dormitory.getDid();
//                        Long id = student.getId();
//                        Sd sd1 = new Sd();
//                        sd1.setSid(id);
//                        sd1.setDid(did);
//                        sdService.save(sd1);
//                        break;
//                    }
//                }
//            }
//            //不为空则不执行任何操作
//        }
//        return new R(1, "添加成功", null);
//    }

    @Override
    public R searchDorm(String sno) {
        List<StudentDto> name = new ArrayList<>();
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Student::getSno, sno);
        Student student = studentService.getOne(queryWrapper);
        if (student == null) {
            throw new DormException("查无此信息");
        }
        Long id = student.getId();
        LambdaQueryWrapper<Sd> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Sd::getSid, id);
        Sd sd = sdService.getOne(queryWrapper1);
        if (sd != null) {
            List<Sd> sds = new ArrayList<>();
            Long did = sd.getDid();
            LambdaQueryWrapper<Sd> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(Sd::getDid, did);
            List<Sd> sdList = sdService.list(queryWrapper2);
            for (Sd sd1 : sdList) {
                Long sid = sd1.getSid();
                Student student1 = studentService.getById(sid);
                StudentDto studentDto=new StudentDto();
                studentDto.setSname(student1.getSname());
                studentDto.setProfession(student1.getProfession());
                studentDto.setPhone(student1.getPhone());
                studentDto.setSex(student1.getSex());
                name.add(studentDto);
            }
        }
        return new R(1, "查询宿舍人员成功", name);
    }

    @Override
    @Transactional
    public R assignment() {
        //查出全部男的
        LambdaQueryWrapper<Student> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Student::getSex,"男");
        List<Student> student1 = studentService.list(queryWrapper);


        //查出全部男的
        List<Student> student2 = dormitoryMapper.students();



        //查出全部男生宿舍
        LambdaQueryWrapper<Dormitory> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.eq(Dormitory::getSex,1);
        List<Dormitory> dormitoriesMan = this.list(queryWrapper1);


        //查出全部女生宿舍
        List<Dormitory> dormitoriesWoman = dormitoryMapper.dorms();



        for (Student student : student1) {
            //先判断这个学生是否有宿舍了，并且判断是男是女
            LambdaQueryWrapper<Sd> queryWrapper4 = new LambdaQueryWrapper<>();
            queryWrapper4.eq(Sd::getSid, student.getId());
            Sd sd = sdService.getOne(queryWrapper4);
            //如果为空，为男学生添加宿舍
            if (sd == null) {
                //遍历宿舍，看看哪间宿舍还有剩
                for (Dormitory dormitory : dormitoriesMan) {
                    //如果宿舍当前容量小于最大容量，就把学生加进去
                    if (dormitory.getNowCapacity() < dormitory.getMaxCapacity()) {
                        //把宿舍容量加一
                        dormitory.setNowCapacity(dormitory.getNowCapacity() + 1);
                        this.updateById(dormitory);
                        //添加学生宿舍表
                        Long did = dormitory.getDid();
                        Long id = student.getId();
                        Sd sd1 = new Sd();
                        sd1.setSid(id);
                        sd1.setDid(did);
                        sdService.save(sd1);
                        break;
                    }
                }
            }
            //不为空则不执行任何操作
        }

        for (Student student : student2) {
            //先判断这个学生是否有宿舍了，并且判断是男是女
            LambdaQueryWrapper<Sd> queryWrapper4 = new LambdaQueryWrapper<>();
            queryWrapper4.eq(Sd::getSid, student.getId());
            Sd sd = sdService.getOne(queryWrapper4);
            //如果为空，为女学生添加宿舍
            if (sd == null) {
                //遍历宿舍，看看哪间宿舍还有剩
                for (Dormitory dormitory : dormitoriesWoman) {
                    //如果宿舍当前容量小于最大容量，就把学生加进去
                    if (dormitory.getNowCapacity() < dormitory.getMaxCapacity()) {
                        //把宿舍容量加一
                        dormitory.setNowCapacity(dormitory.getNowCapacity() + 1);
                        this.updateById(dormitory);
                        //添加学生宿舍表
                        Long did = dormitory.getDid();
                        Long id = student.getId();
                        Sd sd1 = new Sd();
                        sd1.setSid(id);
                        sd1.setDid(did);
                        sdService.save(sd1);
                        break;
                    }
                }
            }
            //不为空则不执行任何操作
        }


        return new R(1, "添加成功", null);
    }
}
